package com.zxkj.commonpatrol.adapt;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.GravityEnum;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.model.entity.DeciveEventEntity;
import com.zxkj.commonpatrol.model.response.DevEvtPatrolInfoResponse;
import com.zxkj.commonpatrol.ui.QuestDetatilActivity;

import net.cachapa.expandablelayout.ExpandableLayout;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.List;

public class DeciveEventAdapt extends BaseQuickAdapter<DevEvtPatrolInfoResponse.DevEvtPatrolQuest, BaseViewHolder> {
    private  RecyclerView mRv;
    public int lastPosition=-1;
    private String routeInspectNo;//路线编号
    private String pointNo;//点编号
    private String devEvtNo;//事件或设备编号
    private String questsNo;//问题集编号
    private  int Type;//类型 1  decive  2  event
    public DeciveEventAdapt(int layoutResId, @Nullable List<DevEvtPatrolInfoResponse.DevEvtPatrolQuest> data,
                            RecyclerView recyclerView,
                            String routeInspectNo,
                            String pointNo,
                            String devEvtNo,
                            String questsNo, int type) {
        super(layoutResId, data);
        this.mRv=recyclerView;
        this.routeInspectNo=routeInspectNo;
        this.pointNo=pointNo;
        this.devEvtNo=devEvtNo;
        this.questsNo=questsNo;
        this.Type=type;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder helper, @Nullable DevEvtPatrolInfoResponse.DevEvtPatrolQuest deciveEventEntity) {
        ExpandableLayout expandableLayout=helper.itemView.findViewById(R.id.expandableLayout);
        Log.e("zjq","进来刷新了");
        if (deciveEventEntity.isExpand()){
            expandableLayout.expand();
        }else{
            expandableLayout.collapse();
        }

        LayoutInflater inflater = LayoutInflater.from(getContext());

        LinearLayout llAddView = helper.itemView.findViewById(R.id.ll_quest);
        llAddView.removeAllViews();
          if (deciveEventEntity.Exceptions!=null){

              for (int i = 0; i < deciveEventEntity.Exceptions.size(); i++) {
                  ConstraintLayout layout = (ConstraintLayout) inflater.inflate(
                          R.layout.item_add_quest_list, null).findViewById(R.id.ll_item);

                  TextView textView13 = (TextView)layout.getViewById(R.id.tv_quest);

                  textView13.setText(i+1+"."+deciveEventEntity.Exceptions.get(i));

                  llAddView.addView(layout);
              }


          }

        //0  red   //1  green
        TextView viewById1 = helper.itemView.findViewById(R.id.tv_spection_parent_id);
        TextView viewById2 = helper.itemView.findViewById(R.id.tv_spection_parent_title);
        ImageView viewById3 = helper.itemView.findViewById(R.id.iv_down);

        ImageView iv_call = helper.itemView.findViewById(R.id.iv_call_phone);
        TextView viewById = helper.itemView.findViewById(R.id.tv_spection_parent_id);
        LinearLayout linearLayout = helper.itemView.findViewById(R.id.ll_call_phone);
         if (deciveEventEntity.getQuestPatrolResult()==0){
             viewById2.setTextColor(getContext().getColor(R.color.red));
         }else{
             viewById2.setTextColor(getContext().getColor(R.color.green));
         }
         helper.setText(R.id.tv_spection_parent_id,deciveEventEntity.getQuestIndex()+"");
        helper.setText(R.id.tv_spection_parent_title,deciveEventEntity.getQuestName());

        helper.setText(R.id.tv_ex3,deciveEventEntity.getLastPatrolDateTime()+" "+deciveEventEntity.getLastPatrolUser());
        if (TextUtils.isEmpty(deciveEventEntity.LastPatrolUser)){
            linearLayout.setVisibility(View.GONE);
            viewById3.setVisibility(View.GONE);
        }  else{
            linearLayout.setVisibility(View.VISIBLE);
            viewById3.setVisibility(View.VISIBLE);
        }


        helper.itemView.findViewById(R.id.iv_down).setOnClickListener(v->{
           if (expandableLayout.isExpanded()){
               expandableLayout.collapse();
               deciveEventEntity.setExpand(false);
               viewById3.setImageResource(R.drawable.down);

           }else{
               if (!TextUtils.isEmpty(deciveEventEntity.LastPatrolUser)){
                   viewById3.setImageResource(R.drawable.up);
                   expandableLayout.expand();
                   deciveEventEntity.setExpand(true);
               }


           }


        });
        viewById2.setOnClickListener(v->{
            Intent intent =new Intent(getContext(), QuestDetatilActivity.class);
            intent.putExtra("RouteInspectNo",routeInspectNo);
            intent.putExtra("PointNo",pointNo);
            intent.putExtra("QuestNo",deciveEventEntity.QuestCode);//问题编号
            intent.putExtra("DevEvtNo",devEvtNo);
            intent.putExtra("name","問題"+viewById1.getText());
            intent.putExtra("QuestName",deciveEventEntity.getQuestName());
            intent.putExtra("QuestsNo",questsNo);//问题集编号
            intent.putExtra("Type",Type);//类型
            getContext().startActivity(intent);

        });

        iv_call.setOnClickListener(v->{
               if (!TextUtils.isEmpty(deciveEventEntity.LastPatrolUserPhone)){

                   new MaterialDialog.Builder(getContext())
                           .content("是否撥打"+deciveEventEntity.LastPatrolUserPhone+"?")
                           .contentGravity(GravityEnum.CENTER)
                           .negativeColor(getContext().getResources().getColor(R.color.colorPrimary))
                           .positiveColor(getContext().getColor(R.color.colorPrimary))
                           .buttonsGravity(GravityEnum.CENTER)
                           .positiveText("是")
                           .negativeText("否")
                           .onPositive(new MaterialDialog.SingleButtonCallback() {
                               @Override
                               public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                            ActivityTool.getInstance().finishAllActivities();
//                          ARouter.getInstance().build("/Login/LoginActivity").navigation(getContext().getApplicationContext());
                                   Intent intent = new Intent(Intent.ACTION_CALL);
                                   Uri data = Uri.parse("tel:" + deciveEventEntity.LastPatrolUserPhone);
                                   intent.setData(data);
                                   getContext().startActivity(intent);

                               }
                           }).show();


               }

        });


    }
}
